/* BeerScreen, Copyright (C) 2009-2010  Gabriel Tremblay <initnull@gmail.com>
 * http://www.etrange.ca/beerscreen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.etrange.beerscreen.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ca.etrange.beerscreen.model.Beer;
import ca.etrange.beerscreen.model.Beer.BEER_SPECS;

// Parse BeerXML Format and store the parsed infos in a beer model
// Note: this format does not uses uri's and localnames, it is namespace agnostic.
public class BeerXMLParser extends DefaultHandler {
	// Const
	private final int RECIPE_TAG_DEPTH = 2;
	
	// Model
	private final Beer beer;
	
	// Parser logic
	private String currentString;
	private boolean concatText;
	private final boolean recordText = true;
	
	// Xml position
	private int tagDepth = 0;
	private boolean styleSection;
	private boolean yeastSection;
	private boolean mashSection;
	
	public BeerXMLParser(final Beer beer){
		this.beer = beer;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		// Don't allocate a string if we don't care about the current text value
		if(recordText == true ){
			// Some text is multi-line and calls the characters function for each text line.
			if(concatText == true){
				currentString += new String(ch, start, length);
			} else {
				currentString = new String(ch, start, length);
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// Leaving the tag
		this.tagDepth--;
		
		if(qName.equalsIgnoreCase("name") == true && tagDepth == 2){
			// Beer name
			beer.setBeerName(currentString);
		} else if (qName.equalsIgnoreCase("date") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.BREW_DATE, currentString);
		} else if (qName.equalsIgnoreCase("og") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.OG, currentString.substring(0, 5));
		} else if (qName.equalsIgnoreCase("fg") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.FG, currentString.substring(0, 5));
		} else if (qName.equalsIgnoreCase("boil_time") && tagDepth == 2) {
			beer.setBeerSpec(BEER_SPECS.BOIL_TIME, currentString + " mins");
		} else if (styleSection == true && qName.equalsIgnoreCase("name") == true){
			beer.setBeerSpec(BEER_SPECS.STYLE, currentString);
		} else if (yeastSection == true && qName.equalsIgnoreCase("name") == true){
			beer.setBeerSpec(BEER_SPECS.YEAST, currentString);
		} else if (qName.equalsIgnoreCase("abv") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.ABV, currentString);
		} else if (qName.equalsIgnoreCase("IBU") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.IBU, currentString);
		} else if (qName.equalsIgnoreCase("DISPLAY_PRIMARY_TEMP") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.FERMENT_TEMP, currentString);
		} else if (qName.equalsIgnoreCase("DISPLAY_PRIMARY_TEMP") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.FERMENT_TEMP, currentString);
		} else if (mashSection == true && qName.equalsIgnoreCase("STEP_TEMP")) {
			beer.setBeerSpec(BEER_SPECS.MASH_TEMP, currentString.substring(0, 4));
		} else if (qName.equalsIgnoreCase("NOTES") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.BEER_NOTES, currentString);
		} else if (qName.equalsIgnoreCase("TASTE_NOTES") && tagDepth == 2){
			beer.setBeerSpec(BEER_SPECS.BEER_TASTE, currentString);
		}
		
		// Tag position desactivation
		if(qName.equalsIgnoreCase("style")){
			styleSection = false;
		} else if (qName.equalsIgnoreCase("yeast")){
			yeastSection = false;
		} else if (qName.equalsIgnoreCase("mash")){
			mashSection = false;
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// entering a tag
		this.tagDepth++;
	
		// Tag position activation
		if(qName.equalsIgnoreCase("style")){
			styleSection = true;
		} else if (qName.equalsIgnoreCase("yeast")){
			yeastSection = true;
		} else if (qName.equalsIgnoreCase("mash")){
			mashSection = true;;
		}
	}
}
